//
//  MASettingTableViewCell.m
//  MateAdv
//
//  Created by 茶 on 2023/9/18.
//

#import "MASettingTableViewCell.h"

@interface MASettingTableViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UISwitch *switchView;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UIImageView *arrowView;

@end

@implementation MASettingTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initInterface];
    }
    return self;
}

- (void)initInterface {
    _titleLabel = [UILabel ma_labelWithSuperView:self.contentView text:@"" textFont:MAFontScaleRegular(16) textColor:MAColor(@"#666666") textAlignment:0 makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.left.ma_offset(20);
        make.centerY.mas_equalTo(0);
    }];
}

- (void)setTitle:(NSString *)title {
    _title = title;
    _titleLabel.text = title;
}

- (void)setIndex:(NSUInteger)index {
    _index = index;
    if (index == 0) {
        [self.contentView addSubview:self.switchView];
        [_switchView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.ma_offset(-18);
        }];
        _arrowView.hidden = YES;
        _switchView.hidden = NO;
    } else if (index == 4) {
        _switchView.hidden = YES;
        _arrowView.hidden = NO;
        [self.contentView addSubview:self.arrowView];
        [self.arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.ma_offset(-19);
        }];
    } else {
        if (index == 1) {
            self.detailLabel.text = [NSString stringWithFormat:@"V%@", kApplication.appVersion];
            self.detailLabel.font = MAFontScaleRegular(16);
            self.detailLabel.textColor = MAColor(@"#333333");
        } else if (index == 2) {
            self.detailLabel.text = @"Apple";
            self.detailLabel.font = MAFontScaleRegular(16);
            self.detailLabel.textColor = MAColor(@"#333333");
        } else if (index == 3) {
            self.detailLabel.font = MAFontScaleSemibold(16);
            self.detailLabel.textColor = MAColor(@"#FF0071");
        }
        _arrowView.hidden = YES;
        _switchView.hidden = YES;
    }
}

- (void)setDetailTitle:(NSString *)detailTitle {
    _detailTitle = detailTitle;
    _detailLabel.text = detailTitle;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)switchAction:(UISwitch *)switchView {
    [kUserDefaults setBool:switchView.isOn forKey:kSavedSwitch];
}

- (UISwitch *)switchView {
    if (_switchView) return _switchView;
    UISwitch *switchView = [[UISwitch alloc]init];
    [switchView setOnTintColor:MAColor(@"#FF0071")];
    [switchView setThumbTintColor:[UIColor whiteColor]];
    switchView.layer.cornerRadius = 15.5f;
    switchView.layer.masksToBounds = YES;
    [switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    switchView.on = MAUserBool(kSavedSwitch);
    _switchView = switchView;
    return _switchView;
}

- (UILabel *)detailLabel {
    if (_detailLabel) return _detailLabel;
    _detailLabel = [UILabel ma_labelWithSuperView:self.contentView text:@"" textFont:MAFontScaleRegular(16) textColor:MAColor(@"#333333") textAlignment:NSTextAlignmentRight makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.centerY.mas_equalTo(0);
        make.right.ma_offset(-18);
    }];
    return _detailLabel;
}

- (UIImageView *)arrowView {
    if (_arrowView) return _arrowView;
    _arrowView = [UIImageView initWithName:@"Mine_Arrow"];
    return _arrowView;
}

@end
